<template>
  <div id="home">
    <mavon-editor v-model="value" :toolbars="markdownOption" @save="save" class="editor" />
    <!--提示框-->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <span>已暂存，是否上传？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
  </el-dialog>
      <el-dialog
      :close-on-click-modal="modal"
      :close-on-press-escape="modal"
      :center="isCenter"
      title="请输入密码"
      :visible.sync="dialogFormVisible"
    >
      <div class="input-box">
        <el-input placeholder="请输入密码" v-model="user_password" show-password></el-input>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
/* eslint-disable */
import {mapState} from 'vuex'
import qs from 'qs'
export default {
  name: "editor",
  data() {
    return {
      // markdown的内容
      value: "",
      // dialog
      dialogVisible: false,
      // dialog2
      dialogFormVisible: false,
      user_password: '',
      isCenter: false,
      modal: false,
      // 配置
      markdownOption: {
        bold: true, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        underline: true, // 下划线
        strikethrough: true, // 中划线
        mark: true, // 标记
        superscript: true, // 上角标
        subscript: true, // 下角标
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        code: true, // code
        table: true, // 表格
        fullscreen: false, // 全屏编辑
        readmodel: true, // 沉浸式阅读
        htmlcode: true, // 展示html源码
        help: true, // 帮助
        /* 1.3.5 */
        undo: true, // 上一步
        redo: true, // 下一步
        trash: true, // 清空
        save: true, // 保存（触发events中的save事件）
        /* 1.4.2 */
        navigation: true, // 导航目录
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        subfield: true, // 单双栏模式
        preview: true // 预览
      }
    };
  },
  methods:{
    save(values, render) {
      this.dialogVisible = true
    },
    confirm() {
      this.dialogVisible = false
      this.post()
    },
    closeDialog() {
      this.dialogVisible = false
      this.$router.push('/')
    },
     async post() {
      try {
        let data = await this.$axios({
          method: 'post',
          url: '/post',
          header:{'content-type': 'application/x-www-form-urlencoded'},
          data: qs.stringify({methods:'article',values:this.value})
        })
        // 服务器响应
        if (data.data.code === 1) {
          console.log('done');
          this.$notify({
            title: '提示',
            message: 'markdown上传成功',
            position: 'bottom-right',
            type:'success'
          })
        }
        if (data.data.code === 2) {
         this.$notify({
            title: '提示',
            message: '出现错误！',
            position: 'bottom-right',
            type:'error'
          })
        }
      } catch(err) {
        this.$notify({
            title: '提示',
            message: `出现错误${err}`,
            position: 'bottom-right',
            type:'error'
          })
      }
    },
    checkPwdStatus() {
      if (this.password_status) {
        this.dialogFormVisible = true
      }
    },
    submit() {
      if (this.user_password !== this.password) {
         this.$notify({
           title: '提示',
           message: '密码错误',
           position: 'bottom-right',
           type:'warning'
          })
          return
      }
      this.dialogFormVisible = false
      this.$notify({
          title: '提示',
          message: '验证通过！',
          position: 'bottom-right',
           type:'success'
         })
    },
  },
  computed: {
    ...mapState(['password_status','password', 'filenameURL'])
  },
  mounted() {
    this.checkPwdStatus()
    console.log(this.password_status, this.password)
  }
};
</script>

<style lang="scss" scoped>
.editor {
  position: absolute;
  top: 6%;
  width: 100%;
  min-height: 100vh;
}
</style>
